package com.goods.controller.business;

import com.goods.business.service.CategroyService;
import com.goods.common.error.BusinessException;
import com.goods.common.model.business.ProductCategory;
import com.goods.common.response.ResponseBean;
import com.goods.common.vo.business.ProductCategoryTreeNodeVO;
import com.goods.common.vo.business.ProductCategoryVO;
import com.goods.common.vo.system.PageVO;
import io.swagger.annotations.Api;
import org.aspectj.apache.bcel.classfile.Module;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author XiaoHong
 * @description 物资类别
 * @date 2022/2/16
 */
@RestController
@Api(tags = "物资类别的相关操作")
@RequestMapping("/business/productCategory")
public class CategoryController {

    @Autowired
    private CategroyService categroyService;
    //加载分类数据
    //http://www.localhost:8989/business/productCategory/categoryTree?pageNum=1&pageSize=5
    @GetMapping("/categoryTree")
    public ResponseBean<PageVO<ProductCategoryTreeNodeVO>> categoryTree(@RequestParam(value = "pageNum",required = false,defaultValue = "1") Integer pageNum,
                                                                       @RequestParam(value = "pageSize",required = false,defaultValue = "100") Integer pageSize){
        PageVO<ProductCategoryTreeNodeVO> categoryList = categroyService.getCategoryTree(pageNum,pageSize);
        return ResponseBean.success(categoryList);
    }

    ////加载父级分类数据
    //http://www.localhost:8989/business/productCategory/getParentCategoryTree
    @GetMapping("/getParentCategoryTree")
    public ResponseBean<List<ProductCategoryTreeNodeVO>> getParentCategoryTree(){
        List<ProductCategoryTreeNodeVO> productCategoryTreeNodeVOList = categroyService.getParentCategoryTree();
        return ResponseBean.success(productCategoryTreeNodeVOList);
    }

    //添加分类数据
   //http://www.localhost:8989/business/productCategory/add
    @PostMapping("/add")
    public ResponseBean add(@Validated  @RequestBody ProductCategory productCategory){
        categroyService.add(productCategory);
        return ResponseBean.success();
    }

    //http://www.localhost:8989/business/productCategory/edit/33
    //点击修改按钮进行显示指定分类数据
    @GetMapping("/edit/{productCategroyId}")
    public ResponseBean<ProductCategory> edit(@PathVariable Long productCategroyId){
        ProductCategory productCategory = categroyService.edit(productCategroyId);
        return ResponseBean.success(productCategory);//
    }

    //http://www.localhost:8989/business/productCategory/update/33
    //分类数据编辑界面点击修改确认
    @PutMapping("/update/{productCategroyId}")
    public ResponseBean update(@RequestBody ProductCategory productCategory,
                               @PathVariable Long productCategroyId){

        categroyService.update(productCategory,productCategroyId);

        return ResponseBean.success();
    }

    //http://www.localhost:8989/business/productCategory/delete/40
    //删除分类数据
    @DeleteMapping("/delete/{productCategroyId}")
    public ResponseBean delete(@PathVariable Long productCategroyId) throws BusinessException {

        categroyService.delete(productCategroyId);
        return ResponseBean.success();
    }

//    //获取物资类别
////    business/productCategory/categoryTree  list
//    @GetMapping("/categoryTree")
//    public ResponseBean<PageVo<ProductCategoryTreeNodeVO>> getParentCategoryTree(){
//        List<ProductCategoryTreeNodeVO> productCategoryTreeNodeVOList = categroyService.getParentCategoryTree();
//        return ResponseBean.success(productCategoryTreeNodeVOList);
//    }


}
